<?php
/**
 * Created by PhpStorm.
 * User: fengbob
 * Date: 2018/1/22
 * Time: 12:14
 */

namespace app\models;


use app\librarys\DbCacheUtility;
use app\librarys\Tools;

class CategoryBak
{

    /**
     * 添加分类
     * @param $parent_id 父节点 根为0
     * @param $name
     * @param $mini_program_id
     * @param $code
     * @param $image_url
     * @param $is_show
     * @param $sort
     * @return mixed
     */
    public static function add($parent_id,$name,$mini_program_id,$code,$image_url,$is_show,$sort){
        $pid = Tools::db_uuid();
        $sql = 'insert into t_goods_category(id,mini_programs_id,`code`,`name`, parent_id,image_url,is_show,sort)
values(:pid,:mini_programs_id,:code,:name,:parent_id,:image_url,:is_show,:sort)';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':pid',$pid);
        $cmd->bindValue(':mini_programs_id',$mini_program_id);
        $cmd->bindValue(':code',$code);
        $cmd->bindValue(':name',$name);
        $cmd->bindValue(':parent_id',$parent_id);
        $cmd->bindValue(':image_url',$image_url);
        $cmd->bindValue(':is_show',$is_show);
        $cmd->bindValue(':sort',$sort);

        $re = DbCacheUtility::execute($cmd);
        return $re;
    }

    /**
     * 返回当前小程序所有分类
     * @param $mini_program_id
     */
    public static function getAll($mini_program_id){
        $sql = 'select * from t_goods_category where $mini_program_id=:mini_program_id';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':mini_program_id',$mini_program_id);

        $re = DbCacheUtility::queryAll($cmd);
    }


    /**
     * 删除分类，同时删除此分类下所有子分类
     * @param $categroy_id
     * @throws \Exception
     */
    public  static function delete($categroy_id){
        $transaction = \Yii::$app->db->beginTransaction();
        try {

            $sql = ' delete from t_goods_category where id=:id';
            $cmd = \Yii::$app->db->createCommand($sql);
            $cmd->bindValue(':id', $categroy_id);

            $re = DbCacheUtility::execute($cmd);
        }catch (\Exception $e) {
            $transaction->rollBack();
            throw $e;
        }
    }

    /**
     * 设置分类可见，不可见
     * @param $category_id
     */
    public static function setIsShow($category_id,$is_show)
    {
        $sql = ' update t_goods_category set is_show=:is_sh where id=:id';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':id', $category_id);
        $is_sh = 1;
        if(!$is_show)
            $is_sh = 0;
        $cmd->bindValue(':id', $is_sh);

        $re = DbCacheUtility::execute($cmd);
        return $re;
    }
}